package com.xl.yygh.cmn.controller;

import com.xl.yygh.cmn.service.DictService;
import com.xl.yygh.common.result.Result;
import com.xl.yygh.model.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author XiangLiang
 * @date 2023/10/23 15:05
 * @description 数据字典Controller
 */
@Api(description = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Resource
    private DictService dictService;

    /**
     * 根据数据id查询子数据列表
     *
     * @param id 节点ID
     */
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    /**
     * 数据字典导出
     *
     * @param response Http请求
     */
    @ApiOperation(value = "数据字典导出")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    /**
     * 数据字典导入
     *
     * @param file 导入的多媒体文件
     * @return 导入结果
     */
    @ApiOperation("数据字典导入")
    @PostMapping("/importData")
    public Result importDictData(MultipartFile file) {
        dictService.importDictData(file);
        return Result.ok();
    }

    /**
     * 根据字典code和value获取字典name
     *
     * @param parentDictCode 字典code
     * @param value          字典value值
     * @return 字典name
     */
    @ApiOperation(value = "根据字典code和value获取字典name")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(@ApiParam(name = "parentDictCode", value = "上级编码", required = true) @PathVariable("parentDictCode") String parentDictCode,
                          @ApiParam(name = "value", value = "值", required = true) @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
    }

    /**
     * 根据value获取字典name
     *
     * @param value 字典value值
     * @return 字典name
     */
    @ApiOperation(value = "根据value获取字典name")
    @ApiImplicitParam(name = "value", value = "值", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/getName/{value}")
    public String getName(@ApiParam(name = "value", value = "值", required = true) @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue("", value);
    }

    /**
     * 根据dictCode获取下级节点
     *
     * @param dictCode 字典编码
     * @return 节点List
     */
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@PathVariable("dictCode") String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }
}
